package com.zyh.system.supermark1.service.impl;

import com.zyh.system.supermark1.dao.impl.GoodsDaoImpl;
import com.zyh.system.supermark1.domain.BuyCar;
import com.zyh.system.supermark1.domain.Goods;
import com.zyh.system.supermark1.service.GoodsService;

import java.util.LinkedList;
import java.util.Scanner;

public class GoodsServiceImpl implements GoodsService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final BuyCarServiceImpl carServiceImpl = new BuyCarServiceImpl();
    public static final LinkedList<Goods> GOODS_LIST = GoodsDaoImpl.GOODS_LIST;

    public void findAll() {
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
        for (Goods goods : GOODS_LIST) {
            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getStock());
        }
    }

    @Override
    public void shopping(LinkedList<Goods> GOODS_LIST) {
        int sumNum = 0;
        outer:
        while (true) {
            findAll();
            System.out.println("请输入商品编号：");
            String id = INPUT.next();
            boolean flag = false;
            for (Goods goods : GOODS_LIST) {
                if (goods.getId().equals(id)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                //获取用户输入编号对应商品的库存
                for (Goods goods : GOODS_LIST) {
                    if (goods.getId().equals(id)) {
                        if (goods.getStock() <= 0) {
                            goods.setStock(0);
                            System.out.println("库存为0，无法购买，等待补货");
                            continue;
                        }
                        while (true) {
                            System.out.println("请输入购买数量：");
                            Integer buyNum = INPUT.nextInt();
                            if (buyNum <= 0) {
                                System.out.println("购买数量不能为零或者负数");
                                continue;
                            }
                            if (buyNum > goods.getStock()) {
                                System.out.println("购买数量必须小于" + goods.getStock());
                                continue;
                            } else {
                                sumNum += buyNum;
                                goods.setStock(goods.getStock() - buyNum);
                                System.out.println("您还要继续购买吗？(是y 否任意键)");
                                INPUT.nextLine();
                                String choice = INPUT.nextLine();
                                switch (choice) {
                                    case "y":
                                        BuyCar buyCar = new BuyCar(goods.getId(), goods.getName(), goods.getPrice(), buyNum);
                                        carServiceImpl.add(buyCar);
                                        continue outer;
                                    default:
                                        BuyCar buyCar1 = new BuyCar(goods.getId(), goods.getName(), goods.getPrice(), buyNum);
                                        carServiceImpl.add(buyCar1);
                                        return;
                                }
                            }
                        }
                    }
                }
            } else {
                System.out.println("您输入的商品编号不存在");
                continue outer;
            }
        }
    }
}